from functools import wraps

from flask import render_template, request, flash, redirect, url_for, session
from flask_login import login_required, current_user, login_user, logout_user

from app import app,login_manager
from app.model import *
from flask import jsonify, request
from db import db
import uuid
from flask import render_template


# @app.route('/equipment', methods=['GET'])
def get_equipment():
    print("----------------get_equipment---------------------")
    equipment_name = request.args.get('equipment_name')
    page = request.args.get('page', 1, type=int)
    per_page = request.args.get('per_page', 10, type=int)

    if equipment_name:
        equipments = Equipment.query.filter_by(equipment_name=equipment_name).paginate(page=page, per_page=per_page)
    else:
        equipments = Equipment.query.paginate(page=page, per_page=per_page)

    equipment_list = [
        {'equipment_id': equipment.equipment_id, 'equipment_name': equipment.equipment_name, 'price': equipment.price, 'configuration': equipment.configuration,'supplier':equipment.supplier,'appearance_date':equipment.appearance_date,'laboratory_name':equipment.laboratory_name} for
        equipment in equipments.items]
    pagination_info = {
        'current_page': equipments.page,
        'total_pages': equipments.pages,
        'items_per_page': equipments.per_page,
        'total_items': equipments.total
    }
    return {
        'equipments': equipment_list,
        'pagination': pagination_info
    }




    # equipment_data = [eq.get() for eq in equipments.items]
    # return {
    #     "eq_list": equipment_data,
    #     'total': equipments.total,
    #     'pages': equipments.pages,
    #     'status': 200
    # }


@app.route('/equipment/insert', methods=['POST'])
def insert_equipment():
    print("----------------insert_equipment---------------------")

    # eq_list = request.json.get("eq_list", [])
    equipment_name = request.json.get('equipment_name'),
    price = request.json.get('price'),
    configuration = request.json.get('configuration'),
    supplier = request.json.get('supplier'),
    appearance_date = request.json.get('appearance_date'),
    laboratory_name = request.json.get('laboratory_name')
    num = request.json.get('num')
    for i in range(1, int(num) + 1):
        equi = Equipment(equipment_id=uuid.uuid4(), equipment_name=equipment_name, price=price,
                         configuration=configuration, supplier=supplier, appearance_date=appearance_date,
                         laboratory_name=laboratory_name)
        db.session.add(equi)
        db.session.commit()
    return jsonify({
        'msg': '插入成功'
    }), 201
    # eq_objs = [Equipment(
    #     ) for data in eq_list]
    # db.session.add_all(eq_objs)
    # try:

    # except Exception as e:
    #     db.session.rollback()
    #     return jsonify({"error": str(e)}), 400


@app.route('/equipment/del', methods=['POST'])
def del_equipment():
    print("----------------del_equipment---------------------")
    eq_list = request.json.get("eq_id_list", [])
    eq_objs = [Equipment.query.filter_by(equipment_id=id).delete() for id in eq_list]
    try:
        db.session.commit()
        return jsonify({
            'msg': '删除成功'
        }), 201
    except Exception as e:
        db.session.rollback()
        return jsonify({"error": str(e)}), 500


@app.route('/equipment/update', methods=['POST'])
def update_equipment():
    print("----------------update_equipment---------------------")
    eq_list = request.json.get("eq_list", [])
    for data in eq_list:
        query = Equipment.query.filter_by(equipment_id=data.get('equipment_id'))
        query.update({
            "equipment_name": data.get('equipment_name'),
            "price": data.get('price'),
            "configuration": data.get('configuration'),
            "supplier": data.get('supplier'),
            "appearance_date": data.get('appearance_date'),
            "laboratory_name": data.get('laboratory_name')
        })

    try:
        db.session.commit()
        return jsonify({
            'msg': '修改成功'
        }), 201
    except Exception as e:
        db.session.rollback()
        return jsonify({"error": str(e)}), 500
